using System;
using System.Diagnostics;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace Framework
{
    public partial class ToolsMenu
    {
        public static void OpenFolder(string folder)
        {
            switch (Application.platform)
            {
                case RuntimePlatform.WindowsEditor:
                    folder = folder.Replace('/', '\\');
                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }
                    Process.Start("Explorer.exe", folder);
                    break;
                case RuntimePlatform.OSXEditor:
                    folder = string.Format("\"{0}\"", folder);
                    Process.Start("open", folder);
                    break;
                default:
                    throw new Exception(
                        string.Format("Opening folder on '{0}' platform is not supported.", Application.platform.ToString()));
            }
        }
        [MenuItem("Window/Folder/Open Data")]
        public static void OpenDataPath()
        {
            OpenFolder(Application.dataPath);
        }

        [MenuItem("Window/Folder/Open PersistentData")]
        public static void OpenPersistentDataPath()
        {
            OpenFolder(Application.persistentDataPath);
        }

        [MenuItem("Window/Folder/Open StreamingAssets")]
        public static void OpenStreamingAssetsPath()
        {
            OpenFolder(Application.streamingAssetsPath);
        }
        // [MenuItem("Framework/Folder/Open Resources Path")]
        // private static void OpenResourcesPath()
        // {
        //     OpenFolder(Path.Combine(Application.dataPath,"Resources"));
        // }
        [MenuItem("Window/Folder/Open TemporaryCache")]
        public static void OpenTemporaryCachePath()
        {
            OpenFolder(Application.temporaryCachePath);
        }
        /*
        [MenuItem("Window/Folder/Clean PersistentData")]
        public static void CleanPersistentDataPath()
        {
            InternalCleanFolder(Application.persistentDataPath);
        }

        [MenuItem("Window/Folder/Clean TemporaryCache")]
        public static void CleanTemporaryCachePath()
        {
            InternalCleanFolder(Application.temporaryCachePath);
        }
        [MenuItem("Window/Folder/Clean StreamingAssets")]
        public static void CleanStreamingPath()
        {
            InternalCleanFolder(Application.streamingAssetsPath);
        }
        */
        public static void InternalCleanFolder(string path)
        {
            // if (!EditorUtility.DisplayDialog("Confirm!", string.Format("Are you sure to clean folder '{0}'", path), "Yes"))
            // {
            //     return;
            // }
            if(!Directory.Exists(path))
            {
                return;
            }
            try
            {
                foreach (var file in Directory.GetFiles(path))
                {
                    File.Delete(file);
                }

                foreach (var dir in Directory.GetDirectories(path))
                {
                    Directory.Delete(dir, true);
                }

                UnityEngine.Debug.Log(string.Format("Folder '{0}' has been cleaned.", path));
            }
            catch (IOException e)
            {
                EditorUtility.DisplayDialog(e.Message, e.StackTrace, "Okay");
            }
        }

    }
}